package com.imagination.applet.infrastructure.handler;

import com.imagination.applet.infrastructure.common.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(value = BadRequestException.class)
    public ResponseEntity<RestResponse<Void>> handleBadRequestException(BadRequestException e) {
        log.error("Occurred bad request exception,", e);
//        ErrorResponse errorResponse = new ErrorResponse(e.getMessage());
        return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
    }


    @ExceptionHandler(value = RuntimeException.class)
    public ResponseEntity<RestResponse<Void>> handleRuntimeException(RuntimeException e) {
        log.info("Occurred runtime exception,", e);
        return new ResponseEntity<>(RestResponse.of("500", e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<RestResponse<Void>> handleException(Exception e) {
        log.info("Occurred exception,", e);
        return new ResponseEntity<>(RestResponse.of("500", e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
